﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Slippor.Common.PluginFramework;

namespace AutoFavHandlerPlugin
{
    public partial class ConfigForm : Form
    {
        private readonly AutoFavConfig _config;
        public ConfigForm(AutoFavConfig config)
        {
            _config = config;
            InitializeComponent();
            intervalUpDown.SelectedItem = config.ConfigSetting;
            if (intervalUpDown.SelectedItem == null)
            {
                intervalUpDown.Text = config.ConfigSetting.ToString();
            }
        }

        private void buttonConfirm_Click(object sender, EventArgs e)
        {
            if (intervalUpDown.SelectedItem != null && _config.ConfigSetting.ToString() != intervalUpDown.SelectedItem.ToString())
            {
                _config.ConfigSetting = intervalUpDown.SelectedItem.ToString();
                this.DialogResult = DialogResult.OK;
            }
            else if (intervalUpDown.Text != _config.ConfigSetting.ToString())
            {
                TimeSpan timeSpan;
                TimeSpan.TryParse(_config.ConfigSetting.ToString(), out timeSpan);
                TimeSpan.TryParse(intervalUpDown.Text, out timeSpan);
                _config.ConfigSetting = timeSpan.ToString();
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                this.DialogResult = DialogResult.Cancel;
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}
